package com.css.fxfzysx.modules.vabStructure.controller;


import com.css.fxfzysx.base.response.RestResponse;

import com.css.fxfzysx.modules.vabStructure.service.SrcAnalyseService;

import com.css.fxfzysx.util.PlatformObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/srcanalyse")
//@Api("钢筋混凝土结构破坏状态")
public class SrcAnalyseController {
    @Autowired
    private  SrcAnalyseService srcAnalyseService ;
    //易损性分析
    @PostMapping
     public  RestResponse calculateModel(@RequestBody Map listMap){
        RestResponse response = null ;
        try {
            Map<String,List> resultMap =  srcAnalyseService.calculatesResult(listMap);
            response = RestResponse.succeed(resultMap);
        }
        catch (Exception e){
            String errorMsg = "数据异常，计算失败！";
            log.error(errorMsg,e);
            response = RestResponse.fail(e.getMessage());
        }
        return response ;
     }

     //计算之后的数据查询回显
     @GetMapping("querymodel")
     public RestResponse queryModel(String calId){
        RestResponse response = null;
        try {
            response = RestResponse.succeed(srcAnalyseService.queryResult(calId));
        }
        catch (Exception e){
            String errorMsg = "查询异常!";
            log.error(errorMsg,e);
            response = RestResponse.fail(e.getMessage());
        }
        return response;
    }

    //保存接口
    @GetMapping("saveModel")
    public RestResponse saveModel(String calId){
        RestResponse response = null;
        try {
            srcAnalyseService.saveModel(calId);
            response = RestResponse.succeed("保存成功！");
        }catch (Exception e){
            String errorMsg = PlatformObjectUtils.isEmpty(e.getMessage())?"保存失败！":e.getMessage();
            log.error(errorMsg,e);
            response = RestResponse.fail(e.getMessage());
        }

        return response;
    }

    //批量重新计算
    @GetMapping("recalculates")
    public RestResponse reCalculates(String calIds){
        RestResponse response = null;
        try {
            srcAnalyseService.reCalculates(calIds);
            response = RestResponse.succeed("重新计算成功！");
        }catch (Exception e){
            String errorMsg = PlatformObjectUtils.isEmpty(e.getMessage())?"重新计算失败！":e.getMessage();
            log.error(errorMsg,e);
            response = RestResponse.fail(e.getMessage());
        }

        return response;

    }

    @GetMapping("exportcrsbuildings")
    public void  importCrsBuildings(String calIds, HttpServletResponse response){
        srcAnalyseService.importCrsBuildings(calIds,response);
    }


}
